package com.xuebank.nfe.nfecar.adapter;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.wzn.libaray.utils.CommonUtils;

/**
 * Created by Wind_Fantasy on 15/8/20.
 */
public class CommonItemDecoration extends RecyclerView.ItemDecoration {
    private int marginLeft;
    private int height;
    private Paint mPaint;
    private boolean mIsTopMarginEnable;

    public CommonItemDecoration(@ColorInt int color) {
        this(color, 0);
    }

    public CommonItemDecoration(@ColorInt int color, int margin) {
        this(color, margin, false);
    }

    public CommonItemDecoration(@ColorInt int color, int margin, boolean isTopMarginEnable) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(color);
        height = margin;
        mIsTopMarginEnable = isTopMarginEnable;
    }

    /**
     *
     * @param c   画布
     * @param parent  起源
     * @param state  状态
     */
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        //getChildCount  返回一个正整数，表示组中的子数。
        int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            //getChildAt如果在组中不存在位置，则在指定位置或空的视图
            View child = parent.getChildAt(i);
            int bottom = child.getBottom();
            if (marginLeft == 0) {
                marginLeft = child.getPaddingLeft();
            }
            if (height == 0) {
                height = CommonUtils.dp2px(parent.getContext(), 0.5f);
            }
            c.drawLine(marginLeft, bottom, child.getRight() - child.getPaddingRight(), bottom + height, mPaint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int pos = parent.getChildLayoutPosition(view);
        if (!mIsTopMarginEnable && pos < 1) {
            super.getItemOffsets(outRect, view, parent, state);
        } else {
            outRect.top = height;
        }
    }
}
